window.JgBridge = window.JgBridge || function () {
  /**
   * 返回系统版本
   */
  function getSystem() {
    var u = navigator.userAgent;
    var system = "A";
    if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1 || u.indexOf('android') > -1) {
      //安卓手机
      system = "A";
    } else if (u.indexOf('iPhone') > -1 || u.indexOf('ios') > -1) {
      //苹果手机
      system = "I";
    } else if (u.indexOf('Windows Phone') > -1) {
      //W手机
      system = "W";
    }
    return system;
  }

  /**
   * 安装WebViewJavascriptBridge
   *
   * @param {*} callback
   */
  function setupWebViewJavascriptBridge(callback) {
    var system = getSystem();
    if (system === "A") {
      if (window.WebViewJavascriptBridge) {
        callback(window.WebViewJavascriptBridge);
      } else {
        document.addEventListener(
          'WebViewJavascriptBridgeReady'
          , function () {
            callback(window.WebViewJavascriptBridge);
          },
          false
        );
      }
    } else if (system === "I") {
      if (window.WebViewJavascriptBridge) {
        return callback(window.WebViewJavascriptBridge);
      }
      if (window.WVJBCallbacks) {
        return window.WVJBCallbacks.push(callback);
      }
      window.WVJBCallbacks = [callback];
      var wvjbIframe = document.createElement('iframe');
      wvjbIframe.style.display = 'none';
      wvjbIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
      document.documentElement.appendChild(wvjbIframe);
      setTimeout(function () { document.documentElement.removeChild(wvjbIframe) }, 0);
    }
    return this;
  }

  setupWebViewJavascriptBridge(function (bridge) {
    bridge.init(function (message, responseCallback) {
      responseCallback(data);
    });
  });

  var call = function (name, data, callback) {
    setupWebViewJavascriptBridge(function (bridge) {
      bridge.callHandler(name, data, function (response) {
        var json = eval('(' + response + ')');
        if (json.state === 1) {
          callback.success(json.content);
        } else {
          callback.fail(json.content);
        }
      });
    });
  }

  return {
    Camera: function (o) { call("Camera", { MethodName: o.MethodName }, o); },
    Photo: function (o) { call("Photo", { MethodName: o.MethodName }, o); },
    QRCode: function (o) { call("QRCode", { MethodName: o.MethodName }, o); },
    GetLoginInfo: function (o) { call("GetLoginInfo", { MethodName: o.MethodName }, o); },
    SetLoginInfo: function (o) { call("SetLoginInfo", { MethodName: o.MethodName, loginInfo: o.loginInfo, }, o); },
    ReLogin: function (o) { call("ReLogin", { MethodName: o.MethodName }, o); },
    H5Verify: function (o) { call("H5Verify", { MethodName: o.MethodName, params: o.params }, o); },
    H5Auth: function (o) { call("H5Auth", { MethodName: o.MethodName }, o); }
  }
}();



